<?php

if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}


$mpurl = 'admin.php?ac=diaryTopicOfficial';
$list = array();
if($_GET['op'] == 'removeDiary'){
	$info = array(
			'do'     	=> 'removeDiary',
			'format' 	=> 'json',
			'bodyId'  	=> intval($_GET['bodyid']),
			'uid'		=> intval($_GET['uid'])	
	);

	$content = get_api_content($_SC ['apiurl'], $info);
	$data = $content['resultData'];
	if($data){
		echo 1;exit;
	}else{
		echo 0;exit;
	}
}
else
//获取一个话题的点滴
if($_GET['op'] == 'getDiarysByTopic'){
	$info = array(
			'do'     	=> 'getDiarysByTopic',
			'format' 	=> 'json',
			'start'		=> empty($_GET['start']) ? 0 : $_GET['start'],
			'length'	=> empty($_GET['length']) ? 50 : $_GET['length'],
			'topic_id'  => intval($_GET['gid']),
	);
	
	$content = get_api_content($_SC ['apiurl'], $info);
	
	$result = json_decode($content,true);
	$list = $result['resultData'];
	
	$page_len = array();
	$page_len = array(intval($info['length'])=>'selected');
	
	$mpurl  = 'admin.php?ac=diaryTopicOfficial&op=getDiarysByTopic&gid='.$info['topic_id'].'&length='.$info['length'];
	$pageurl = multi($info['start'], $info['length'], $mpurl);
	$nextpurl  = $pageurl ['next'];
	$prevpurl  = $pageurl ['prev'];
	$mpurl     = $pageurl ['urls'];
	$firstpurl = $pageurl ['first'];
	include_once template("admin/topicDiary");
	exit;
}else
//编辑热门话题
if($_GET['op'] == 'editDiaryTopicInfo'){
	$info = array(
			'do'     	=> 'editDiaryTopicInfo',
			'format' 	=> 'json',
			'id'     	=> intval($_GET['id']),
			'type'   	=> intval($_GET['type']),
			'hot'		=> intval($_GET['hot']),
			'img_small' => strval($_GET['img_small']),
			'banner'	=> ''
	);
	
	$arr_img 	= explode(',', trim($_GET['banner']));
	$arr_action = explode(',', trim($_GET['action']));
	
	if(trim($_GET['banner'])){					
		for($i = 0 ; $i<count($arr_img) ; $i++){
			$data[] = array(
					'id'		=>intval($_GET['id']),
					'image'		=>$arr_img[$i],
					'action'	=>$arr_action[$i]
			);
		}	
		$info['banner'] = var_json_encode($data);
	}
   
	$content = get_api_content($_SC ['apiurl'], $info);
	
	$result = json_decode($content,true);
	$result = $result['resultData'];	
	
	echo '<script>window.location.href="admin.php?ac=diaryTopicOfficial"</script>';
	exit;
	//搜索话题
}else if($_GET['op'] == 'searchDiaryTopic'){
	$info = array(
			'do'     => 'searchDiaryTopic',
			'format' => 'json',
			'name'   => $_GET['name']
	);
	$content = get_api_content($_SC ['apiurl'], $info);
	
	$result = json_decode($content,true);
	$result = $result['resultData'];
	
	if(empty($result)){
		$result = '';
	}else{
		if(!empty($result['banner'])){
			foreach ($result['banner'] as $v){
				$banner .= $v['image'].',';
				$action .= $v['action'].',';
			}
		}else{
			$banner = '';
			$action = '';
		}
		
		$result['banner'] = trim($banner,',');
		$result['action'] = trim($action,',');
		$result = array($result);
	}
		
	include_once template("admin/diaryTopicOfficial");

	//设置热门
}else if($_GET['op'] == 'editDiaryTopicType'){
	$info = array(
			'do'     => 'editDiaryTopicType',
			'format' => 'json',
			'id'     => intval($_GET['id']),
			'type'   => intval($_GET['type']),
			'hot'	 => intval($_GET['hot'])
	);
	$content = get_api_content($_SC ['apiurl'], $info);
	
	$result = json_decode($content,true);
	if($result['resultData']){
		echo '<script>alert("操作成功~");window.location.href="admin.php?ac=diaryTopicOfficial&type=all"</script>';
		exit;
	}else{
		echo '<script>alert("操作失败~");window.location.href="admin.php?ac=diaryTopicOfficial&type=all"</script>';
		exit;
	}
	//删除话题
}else if($_GET['op']=='deleteDiaryTopic'){
	$param = array(
			'do'	 => 'deleteDiaryTopic',
			'format' => 'json',
			'gid'    => empty($_GET['gid'])   ? 0:$_GET['gid'],
			'status' => 3,//empty($_GET['status'])? 2:$_GET['status'], 	
			'type'	 => intval($_GET['type'])		
	);

	$content = get_api_content($_SC ['apiurl'], $param);	
	$result = json_decode($content,true);
	if($result['resultData']){
		echo '<script>alert("操作成功~");window.location.href="admin.php?ac=diaryTopicOfficial&type=all"</script>';
		exit;
	}else{
		echo '<script>alert("操作失败~");window.location.href="admin.php?ac=diaryTopicOfficial&type=all"</script>';
		exit;
	}
}else{
	//获取列表
	$param = array(
		'do'	 => 'getDiaryTopic',
		'format' => 'json',
		'start'  => empty($_GET['start'])  ? 0  : $_GET['start'],
		'length' => empty($_GET['length']) ? 50 : $_GET['length'],
		'status' => empty($_GET['status']) ? 1  : $_GET['status'],
		'type'   => !isset($_GET['type'])  ? 1  : $_GET['type'],
		'sort'   => empty($_GET['sort']) ? ''   : $_GET['sort']
 	);	
	
	$content = get_api_content($_SC ['apiurl'], $param);
	
	$data = json_decode($content,true);
	
	$r = $data['resultData'];
	if(empty($r)){
		$result = '';
	}else{
		foreach ($r as &$val){			
			if(!empty($val['banner'])){				
				foreach ($val['banner'] as $v){					
					$banner .= $v['image'].',';					
					$action .= $v['action'].',';
				}
			}else{
				$banner = '';					
				$action = '';
			}
		
			$val['banner'] = trim($banner,',');
			$val['action'] = trim($action,',');
		}
		
		$result = $r;		
	}
	
	$sort = array();
	$sort = array(strval($_GET['sort']) =>'selected');
	$page_len = array();
	$page_len = array(intval($_GET['length'])=>'selected');
	
	$mpurl  = 'admin.php?ac=diaryTopicOfficial&type='.$param['type'].'&sort='.$param['sort'];
	$pageurl = multi($param['start'], $param['length'], $mpurl);
	$nextpurl  = $pageurl ['next'];
	$prevpurl  = $pageurl ['prev'];
	$mpurl     = $pageurl ['urls'];
	$firstpurl = $pageurl ['first'];
	include_once template("admin/diaryTopicOfficial");
}








